package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.itheima.service.CrowdService;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author GJT
 */
@RestController
@RequestMapping("crowd")
public class CrowdController {

    @Reference(version = "1.0.0")
    private CrowdService crowdService;

    /**
     * 分页查询方法
     * @param queryPageBean 分页参数实体类
     * @return  分页查询结果，总条数与当前页数据
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = crowdService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }











   /* *//**
     * 新增检查项
     * @param checkItem 检查项实例
     * @return  新增操作结果
     *//*
    @RequestMapping("add")
    public Result add(@RequestBody @Validated CheckItem checkItem, Errors errors){
        if(errors.hasErrors()){
            StringBuilder msg = new StringBuilder();
            List<FieldError> fieldErrors = errors.getFieldErrors();
            for (int i = 0; i < fieldErrors.size(); i++) {
                if(i!=fieldErrors.size()-1) {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append(",");
                }else {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append("!");
                }
            }
            return new Result(false, msg.toString());
        }
        //调用业务层添加方法
        checkItemService.add(checkItem);
        //返回操作结果
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    *//**
     * 编辑
     * @param checkItem 检查项实体类
     * @return  编辑操作的结果
     *//*
    @RequestMapping("update")
    public Result update(@RequestBody CheckItem checkItem){
        //调用业务层修改方法
        checkItemService.update(checkItem);
        //返回操作结果
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    *//**
     * 删除
     * @param id 要删除的检查项的ID
     * @return  编辑操作的结果
     *//*
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        checkItemService.delete(id);
        //返回操作结果
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }



    *//**
     * 查询全部检查项
     * @return  返回结果
     *//*
    @Cacheable(expire = 600)
    @RequestMapping("findAll")
    public Result findAll(){
        List<CheckItem> list = checkItemService.findAll();
        if (list==null||list.size()<=0){
            throw new BusinessException("查询失败，请联系管理员");
        }
        System.out.println("这里是查询所有检查项，我从数据库查询了！");
        return new Result(true,"查询成功",list);
    }*/
}
